<template>
  <div class="user-wrapper">
    <div class="content-box">
      <a-dropdown>
        <span class="action ant-dropdown-link user-dropdown-menu">
          <a-avatar class="avatar" size="small" :src="avatar()" />
          <span>{{ nickname() }}</span>
        </span>
        <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
          <a-menu-item key="1">
            <router-link :to="{ name: 'settings' }">
              <a-icon type="setting" />
              <span>账户设置</span>
            </router-link>
          </a-menu-item>
          <a-menu-divider />
          <a-menu-item key="3">
            <a href="javascript:;" @click="comfirmMessage()">
              <a-icon type="logout" />
              <span>退出登录</span>
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
      <a-modal title="提示" :visible="visible" :confirm-loading="confirmLoading" @ok="handleOk" @cancel="handleCancel">
        <p>{{ ModalText }}</p>
      </a-modal>
    </div>
  </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'

export default {
  name: 'UserMenu',
  data() {
    return {
      visible: false,
      confirmLoading: false,
      ModalText: '是否确认退出当前登录？',
    }
  },
  methods: {
    ...mapActions(['Logout']), // 清除token和localStorage中的信息
    ...mapGetters(['nickname', 'avatar']), // 从全局变量中获取用户昵称和头像
    comfirmMessage() {
      this.visible = true;
    },
    handleOk(e) {
      const that = this
      return that.Logout({}).then(() => {
        window.location.reload()
      }).catch(err => {
        that.$message.error({
          title: '错误',
          description: err.message
        })
      })
    },
    handleCancel(e) {
      console.log('Clicked cancel button');
      this.visible = false;
    },
    handleLogout() {
      console.log("准备退出")
      const that = this
      this.$confirm({
        title: '提示',
        content: '真的要注销登录吗 ?',
        onOk() {
          console.log("执行logout")
          return that.Logout({}).then(() => {
            location.href = '/'
            //window.location.reload()
          }).catch(err => {
            that.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel() {
        }
      })
    },
    handleLogout1() {
      const that = this
      this.$confirm({
        title: '提示',
        content: '真的要注销登录吗 ?',
        onOk() {
          return that.Logout({}).then(() => {
            location.href = '/'
            //window.location.reload()
          }).catch(err => {
            that.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel() {
        }
      })
    }
  }
}
</script>
